

project , original("../data_input/ratings/state_ratings_merged_into_klarnerID.dta")


	**********
	** 
	** 
	** 
	**********	
	
	
	use "../data_input/ratings/state_ratings_merged_into_klarnerID.dta", clear
	rename klarner_id CandId
	keep if regexm(state, "AR|AZ|CA|CO|FL|LA|ME|MI|MO|MT|NV|OH|OK|SD" )
	gen dem = cand_party=="Dem"
	
	qui foreach v of varlist rating* sig_name* sig_issue* economic_issues* social_issues* { //
		sum `v'
		local n = r(N)
		if `n' >= 500 { // drop if total observations is less than 500
			reg `v' dem
			replace `v' =100-`v' if _b[dem]>0 // if coefficient on dem is positive, Dems score higher => flip so higher numbers = more conservative
			gen match = `v'!=.
			bys state term: egen matchrate_`v' = mean(match)
			drop match
		}
		
		else if `n' < 500 {
		drop `v'
		}
		
	}
		
	keep CandId term *rating* sig_name* sig_issue* economic_issues* social_issues*
		

	save "../data_output/ratings.dta", replace
	project, creates("../data_output/ratings.dta")
